package com.example.yeujian5.yuejian5.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yeujian5.yuejian5.Listenner.MasterDetailListener;
import com.example.yeujian5.yuejian5.R;
import com.example.yeujian5.yuejian5.adapter.MasterDetailAdapter;
import com.example.yeujian5.yuejian5.bean.MasterBean;
import com.example.yeujian5.yuejian5.fragment.QuestionFragment;
import com.example.yeujian5.yuejian5.fragment.VideoFragment;
import com.example.yeujian5.yuejian5.model.MasterModel;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MasterDetailActivity extends AppCompatActivity implements MasterDetailListener,View.OnClickListener{



    private TabLayout tabs;
    private ViewPager viewPager;
    private List<String> mTitle = new ArrayList<String>();
    private List<Fragment> mFragment = new ArrayList<Fragment>();

    private ImageView img;
    private TextView name,intro,place;
    Intent intent;
    public  String Picid;
    public int id;
    public MasterBean list;
    ImageView reg_return;

    SharedPreferences sp;
    public int userid;
    Context context;
    private String result;

    private ImageButton m_collect;
    private TabLayout mTabLayout;
    private ViewPager mviewpage;
    MasterDetailAdapter masterDetailAdapter;
    VideoFragment videoFragment;
    QuestionFragment questionFragment;
    private TabLayout.Tab mastervideo;
    private TabLayout.Tab masterquestion;
    private List<Fragment> fragmentList;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_master_detail);
        intent=getIntent();
        Picid=intent.getStringExtra("id");

        MasterModel masterModel=new MasterModel();
        masterModel.getMasterById(Picid,this);
        context=MasterDetailActivity.this;

        initViews();

        reg_return.setOnClickListener(this);
        m_collect.setOnClickListener(this);
        isCollect();

    }

    public void initViews() {

        /*获取用户*/
        sp=context.getSharedPreferences("Userid",MODE_PRIVATE);
        userid=Integer.valueOf(sp.getString("user_id",null));

        img= (ImageView) findViewById(R.id.img);
        intro= (TextView) findViewById(R.id.intro);
        name= (TextView) findViewById(R.id.name);
        place= (TextView) findViewById(R.id.place);
        m_collect=(ImageButton) findViewById(R.id.love);
        reg_return= (ImageView) findViewById(R.id.reg_return);

        mviewpage= (ViewPager) findViewById(R.id.master_viewPager);
        masterDetailAdapter=new MasterDetailAdapter(getSupportFragmentManager());
        masterDetailAdapter.notifyDataSetChanged();
        mviewpage.setAdapter(masterDetailAdapter);
        //将TabLayout与ViewPager绑定在一起
        mTabLayout= (TabLayout) findViewById(R.id.master_tab02);
        mTabLayout.setupWithViewPager(mviewpage);
        //指定Tab的位置
        mastervideo=mTabLayout.getTabAt(0);
        masterquestion=mTabLayout.getTabAt(1);


    }




    @Override
    public void onResponse(MasterBean bean) {
        list=bean;
        intro.setText(list.getIntroduction());
        name.setText(list.getRealname());
        place.setText(list.getProvince_name()+list.getCity_name()+list.getClub_name());
        if(!list.getThumb().isEmpty())
        {
            Picasso.with(this).load("http://172.24.10.175/workout/Uploads/"+list.getThumb()).into(img);
        }
        else
        {
            img.setImageResource(R.drawable.ic_me);
        }
    }


    @Override
    public void onFail(String msg) {

    }




    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.love:
                collect();
                break;

            case R.id.reg_return:
                finish();

        }
    }



    private void isCollect(){
        MasterModel masterModel = new MasterModel();
        Call<Integer> call = masterModel.isCollected(Picid,userid);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()==0){
                    m_collect.setImageResource(R.drawable.ic_love_press);
                    result="yes";
                }else{
                    m_collect.setImageResource(R.drawable.ic_love);
                    result="no";
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });

    }


    private void collect(){
        if("no".equals(result)){
            MasterModel masterModel = new MasterModel();
            Call<Integer> call = masterModel.userCollectMaster(userid,Picid);
            call.enqueue(new Callback<Integer>() {
                @Override
                public void onResponse(Call<Integer> call, Response<Integer> response) {
                    Toast.makeText(MasterDetailActivity.this, "关注成功！", Toast.LENGTH_SHORT).show();
                    m_collect.setImageResource(R.drawable.ic_love_press);
                    isCollect();
                }

                @Override
                public void onFailure(Call<Integer> call, Throwable t) {

                }
            });
        }
        else{
            MasterModel masterModel = new MasterModel();
            Call<Integer> call = masterModel.changeTeaState1(userid,Picid);
            call.enqueue(new Callback<Integer>() {
                @Override
                public void onResponse(Call<Integer> call, Response<Integer> response) {
                    Toast.makeText(MasterDetailActivity.this,"取消关注成功",Toast.LENGTH_LONG).show();
                    m_collect.setImageResource(R.drawable.ic_love);
                    isCollect();
                }


                @Override
                public void onFailure(Call<Integer> call, Throwable t) {

                }
            });
        }
    }




}

